#include "format.h"

format::format()
{

}

void format::checkFormat(point upperLeft, point lowerLeft, point upperRight, int pixelProportion, QImage *img)
{
    int format = 0;
    int counter = 14;

    pixel p;

    for (int col = 0; col < 8*pixelProportion; col += pixelProportion)
    {
        if (col != 6*pixelProportion)
        {
            if (p.isPixelBlack(upperLeft.getRow()+(5*pixelProportion), upperLeft.getColumn()-(3*pixelProportion)+col, img))
                format += 1 << counter;
            counter--;
            p.paintPixel(upperLeft.getRow()+(5*pixelProportion), upperLeft.getColumn()-(3*pixelProportion)+col, pixelProportion, qRgb(0,0,255), img);
        }
    }

    for (int row = 0; row < 9*pixelProportion; row += pixelProportion)
    {
        if (row != 2*pixelProportion)
        {
            if (p.isPixelBlack(upperLeft.getRow()+(5*pixelProportion)-row, upperLeft.getColumn()+(5*pixelProportion), img))
                format += 1 << counter;
            counter--;
            p.paintPixel(upperLeft.getRow()+(5*pixelProportion)-row, upperLeft.getColumn()+(5*pixelProportion), pixelProportion, qRgb(0,0,255), img);
        }
    }

    int format2 = 0;
    counter = 14;

    for (int row = 0; row < 7*pixelProportion; row += pixelProportion)
    {
        if (p.isPixelBlack(lowerLeft.getRow()+(3*pixelProportion)-row, lowerLeft.getColumn()+(5*pixelProportion), img))
            format2 += 1 << counter;
        counter--;
        p.paintPixel(lowerLeft.getRow()+(3*pixelProportion)-row, lowerLeft.getColumn()+(5*pixelProportion), pixelProportion, qRgb(0,0,255), img);
    }

    for (int col = 0; col < 8*pixelProportion; col += pixelProportion)
    {
        if (p.isPixelBlack(upperRight.getRow()+(5*pixelProportion), upperRight.getColumn()-(4*pixelProportion)+col,img))
            format2 += 1 << counter;
        counter--;
        p.paintPixel(upperRight.getRow()+(5*pixelProportion), upperRight.getColumn()-(4*pixelProportion)+col, pixelProportion, qRgb(0,0,255), img);
    }

    if (format != format2) // assert format number
        format = 0;

    eccLevel = 'E';
    maskPattern = -1;
    switch (format) {
        case L_0:
            maskPattern = 0;
            eccLevel = 'L';
            break;
        case L_1:
            maskPattern = 1;
            eccLevel = 'L';
            break;
        case L_2:
            maskPattern = 2;
            eccLevel = 'L';
            break;
        case L_3:
            maskPattern = 3;
            eccLevel = 'L';
            break;
        case L_4:
            maskPattern = 4;
            eccLevel = 'L';
            break;
        case L_5:
            maskPattern = 5;
            eccLevel = 'L';
            break;
        case L_6:
            maskPattern = 6;
            eccLevel = 'L';
            break;
        case L_7:
            maskPattern = 7;
            eccLevel = 'L';
            break;
    }

    converter c;
    log->addLog("\nECC LEVEL = " + c.charToString(getECCLevel()));
    log->addLog("\nMASCARA = " + c.intToString(getMaskPattern()));
}

void format::setCanvas(canvas *log)
{
    this->log = log;
}

char format::getECCLevel()
{
    return this->eccLevel;
}

int format::getMaskPattern()
{
    return this->maskPattern;
}
